package com.wanpinghui.supplier.user;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.adapter.BusinessAreaAdapter;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.AreaBean;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.storage.ShareDataManager;
import com.yunshl.yunshllibrary.storage.SharedPreferencesKey;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import retrofit.callback.YRequestCallback;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/11/23
 */
@ContentView(R.layout.layout_activity_business_area)
public class BusinessAreaActivity extends BaseActivity {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;

    @ViewInject(R.id.expandableListView)
    private ExpandableListView expandableListView;

    @ViewInject(R.id.btn_submit)
    private ThrottleButton btn_submit;

    private BusinessAreaAdapter businessAreaAdapter;


    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        List<AreaBean> areaBeanList = getIntent().getParcelableArrayListExtra("areaBeanArrayList");
        if (areaBeanList != null && areaBeanList.size() > 0) {
            setDatas(areaBeanList);
        } else {
            showWarningText("获取数据失败");
        }
    }

    private void setDatas(List<AreaBean> areaBeanList) {
        if (areaBeanList != null) {
            businessAreaAdapter = new BusinessAreaAdapter(areaBeanList, BusinessAreaActivity.this);
            expandableListView.setGroupIndicator(null);
            expandableListView.setDivider(null);
            expandableListView.setAdapter(businessAreaAdapter);
        }
    }

    @Override
    public void bindEvents() {
        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_submit.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                List<AreaBean> areaBeanList = businessAreaAdapter.getDatas();
                ArrayList<String> arrayList = new ArrayList<>();
                for (int i = 0; i < areaBeanList.size(); i++) {
                    if (i != 0) {
                        List<AreaBean> areaBean = areaBeanList.get(i).getChildren();
                        for (AreaBean bean : areaBean) {
                            if (bean.isSelect()) {
                                arrayList.add(bean.getAdcode());
                            }
                        }
                    }
                }
                WPHSdk.getService(UserInfoService.class).areasScope(areaBeanList.get(0).isSelect(), arrayList, new YRequestCallback<Object>() {
                    @Override
                    public void onSuccess(Object var1) {
                        syncUserInfo();
                        ShareDataManager.getInstance().save(BusinessAreaActivity.this, SharedPreferencesKey.SELECT_ADDRESS_DATA, new Gson().toJson(businessAreaAdapter.getDatas()));
                        Intent intent = new Intent();
                        intent.putExtra("Areabeanlist", businessAreaAdapter.getDatas());
                        setResult(0, intent);
                        finish();
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                    }
                });
            }
        });
    }

    private void syncUserInfo(){
        WPHSdk.getService(OAuthService.class).getUserInfoFromS(null);
    }

    @Override
    public void initData() {

    }

    private String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}
